

const {ccclass, property} = cc._decorator;

@ccclass
export default class DWC_MsgList extends cc.Component {

    @property(cc.Prefab)
    itemTemplate: cc.Prefab = null;

    @property(cc.ScrollView)
    scrollView:cc.ScrollView = null;

    @property(cc.Integer)
    spawnCount = 7;

    @property(cc.Float)
    spacing = 10;

    @property(cc.Float)
    bufferZone = 400;

    content = null;
    items = [];

    totalCount = 0;
    updateTimer = 0;
    lastContentPosY = 0;
    updateInterval = 0;

    _data  = null;

    start () {

    }


    onEnable(){

    }

    initData(data ){
        this._data = data;
        this.content = this.scrollView.content;
        this.content.height = this.getTotalContentHeight();

        if( this.checkIsNeedInitContent() ){
            this.items = [];
            this.initialize( this._data )
            this.updateTimer = 0;
            this.updateInterval = 0.2;
            this.lastContentPosY = 0;
            return ;
        }else {
            this.trunToBottom();
            return ;
        }



    }

    checkIsNeedInitContent(){

        if(this.content.children.length == this.spawnCount ){
            return false;
        }
        return true;
    }

    //初始化，如果
    initialize(list) {

        // for(let i in this.content.children){
        //     this.content.children[i].active = false;
        // }

        this.totalCount = list.length;
        if(this.totalCount <= 0){
            return ;
        }
        //this.content.height = this.getTotalContentHeight();

        for (let i = 0; i < this.spawnCount && i < this._data.length ; ++i) {

            let item = null;
            if(this.content.children[i]){
                item = this.content.children[i];
            }else {
                item = cc.instantiate(this.itemTemplate);
                item.setParent(this.content);
            }
            item.active = true;

            //item高度不一致的情况
            let y = this.getY(i);
            item.setPosition(0, -y );

            //设置item数据
            item.getComponent(item.name).init( i ,this._data[i] );
            this.items.push(item);
        }
        this.trunToBottom();
    }

    trunToBottom(){
        // let bottomY =  this.getTotalContentHeight() - this.scrollView.node.height > 0 ? this.content.height - this.scrollView.node.height - this.content.y: -this.content.y ;
        // let time = 0.2;
        // this.content.stopAllActions();
        // cc.tween(this.content)
        //     .by(time,{"y":bottomY} )
        //     .start();

    }

    getPositionInView (item) {
        let worldPos = item.parent.convertToWorldSpaceAR(item.position);
        let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
        return viewPos;
    }
    update(dt) {
        return ;
        this.updateTimer += dt;
        if (this.updateTimer < this.updateInterval) return;
        this.updateTimer = 0;
        let items = this.items;
        let buffer = this.bufferZone;
        let isDown = this.scrollView.content.y < this.lastContentPosY;
        //let offset = (this.itemTemplate.data.height + this.spacing) * items.length;
        let offset = this.getOffset();

        for (let i = 0; i < items.length; ++i) {
            let viewPos = this.getPositionInView(items[i]);         //获取每个item的坐标

            //向下滑动
            if (isDown) {
                if (viewPos.y < -buffer && items[i].y + offset < 0) {
                    //items[i].y = items[i].y + offset;
                    //设置item数据
                    let item = items[i].getComponent(this.itemTemplate.data.name);
                    let itemId = item.itemID - items.length;
                    if(itemId < this._data.length  ){
                        item.init( itemId,this._data[itemId]);

                        let y = this.getY(itemId);
                        item.node.setPosition(0, -y );
                    }
                }
            }
            //现上滑动
            else {
                if (viewPos.y > buffer && items[i].y - offset > -this.content.height) {
                    //items[i].y = items[i].y - offset;

                    //设置item数据
                    let item = items[i].getComponent(this.itemTemplate.data.name);
                    let itemId = item.itemID + items.length;
                    if(itemId < this._data.length ){
                        item.init( itemId,this._data[itemId]);

                        let y = this.getY(itemId);
                        item.node.setPosition(0, -y );
                    }
                }
            }
        }
        this.lastContentPosY = this.scrollView.content.y;
    }

    getY(index = 0){

        if(index == 0){
            return 0;
        }

        let allHeight = 0;
        for(let i = 0 ; i < this._data.length ; i ++){
            let str = this._data[i].content;
            let item = this.itemTemplate.data;
            let height = item.getComponent(item.name).getMsgItemHeight(str);
            allHeight += height;
            if( index - 1 == i){
                break;
            }
        }
        return allHeight;
    }

    getOffset(){
        let offset = 0;
        //以前的计算方式，这个现在不能这样用了，因为每一个的高度都不同了
        //(this.itemTemplate.data.height + this.spacing) * items.length;

        for(let i = 0; i < this.items.length ; i ++){
            let itemId = this.items[i].getComponent(this.itemTemplate.data.name).itemID;
            let str = this._data[itemId].content;
            let height = this.items[i].getComponent(this.itemTemplate.data.name).getMsgItemHeight(str);
            offset += height;
        }
        return offset;
    }

    getHeight(index){
        let str = this._data[index].content;
        let item = this.itemTemplate.data;
        let height = item.getComponent(item.name).getMsgItemHeight(str);
        return height;
    }

    getTotalContentHeight(){
        let heightAll = 0;
        for(let i = 0 ; i < this._data.length ; i++){
            let height = this.getHeight(i);
            heightAll += height;
        }
        return heightAll;
    }

}
